<?php
namespace SixShop\News\Controller\Api;

use think\Response;
use think\Request;
use SixShop\News\Service\NewsCategoryService;

class CategoryController
{
    private $service;

    public function __construct(NewsCategoryService $service)
    {
        $this->service = $service;
    }

    /**
     * Display a listing of the resource.
     *
     * @return Response
     */
    public function index(): Response
    {
        $list = $this->service->getList();
        return json($list);
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return Response
     */
    public function read($id): Response
    {
        $item = $this->service->getById($id);
        return json($item);
    }
    
    /**
     * Save a new resource in storage.
     *
     * @param  Request  $request
     * @return Response
     */
    public function save(Request $request): Response
    {
        $data = $request->post();
        $result = $this->service->create($data);
        return json($result);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  Request  $request
     * @param  int  $id
     * @return Response
     */
    public function update(Request $request, $id): Response
    {
        $data = $request->put();
        $result = $this->service->update($id, $data);
        return json($result);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return Response
     */
    public function delete($id): Response
    {
        $result = $this->service->delete($id);
        return json($result);
    }
} 